package com.yuanthink.gwt.client.workplace;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.yuanthink.gwt.client.eventbus.EventBus;

public class ColumnModel extends BaseTreeModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected List<Entry> entries = new ArrayList<Entry>();
	EventBus eventBus = new EventBus();
	
	public ColumnModel() {
		Category test = new Category("会员管理");
		test.add("会员管理", "memberList");
		test.add("修改会员", "memberUpdate");
		add(test);
		
		loadEntries(this);
	}

	public Entry findEntry(String name) {
		if (get(name) != null) {
			return (Entry) get(name);
		}
		for (Entry entry : getEntries()) {
			if (name.equals(entry.getId())) {
				return entry;
			}
		}
		return null;
	}

	public List<Entry> getEntries() {
		return entries;
	}

	private void loadEntries(TreeModel model) {
		for (ModelData child : model.getChildren()) {
			if (child instanceof Entry) {
				entries.add((Entry) child);
			} else if (child instanceof Category) {
				loadEntries((Category) child);
			}
		}
	}
}
